Imports IICBusinessLogic

Public Class StudentGradeGridViewValidator

    Private grdGrades As Grid.CGBaseGrid
    Private ColumnGrade As DataGridViewColumn
    Private ColumnPeriodOpen As DataGridViewColumn
    Private ColumnAttend As DataGridViewColumn

    Public Sub New(ByVal ingrd As Grid.CGBaseGrid)

        Me.grdGrades = ingrd
        Me.ColumnGrade = Me.grdGrades.Columns("Grade")
        Me.ColumnPeriodOpen = Me.grdGrades.Columns("IsOpen")
        Me.ColumnAttend = Me.grdGrades.Columns("Attend")

        If Me.ColumnGrade Is Nothing OrElse Me.ColumnAttend Is Nothing OrElse Me.ColumnPeriodOpen Is Nothing Then
            Throw New ApplicationException("All 3 columns must be defined: Grade,IsOpen,Attend ")
        End If


        Me.ColumnAttend.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.ColumnGrade.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.grdGrades.setEditable(New ClassMemberDBMapper)

        For Each dc As DataGridViewColumn In Me.grdGrades.Columns
            dc.ReadOnly = Not (dc.Name = "Attend" Or dc.Name = "Grade")
        Next


        AddHandler grdGrades.CellValidating, AddressOf CellValidating
        AddHandler grdGrades.CellEndEdit, AddressOf CellEndEdit

        For Each f As Form In Application.OpenForms
            If f Is frmMain Then
                AddHandler grdGrades.RowErrorTextChanged, AddressOf CType(f, frmMain).gridRowErrorTextChanged
                Exit For
            End If
        Next

    End Sub

    Private Sub CellValidating(ByVal sender As Object, _
      ByVal e As DataGridViewCellValidatingEventArgs)

        If Me.grdGrades.DataLoading OrElse Me.grdGrades.IsCurrentCellDirty = False Then Exit Sub

        If grdGrades.Rows(e.RowIndex).Cells(ColumnPeriodOpen.Name).Value.ToString <> "1" Then
            grdGrades.Rows(e.RowIndex).ErrorText = "Can't modify grades for a class in a closed period."
            e.Cancel = True
            Return
        End If

        ' Abort validation if cell is not in the Grade column.
        Dim currentColumn As DataGridViewColumn = Me.grdGrades.Columns(e.ColumnIndex)
        If currentColumn.Name = "Grade" Then

            ' Confirm that the cell's value is a number and that is within the acceptable range.
            If (Not String.IsNullOrEmpty(e.FormattedValue.ToString())) Then

                Dim classId As Integer = CInt(grdGrades.Rows(e.RowIndex).Cells("ClassId").Value)
                Dim periodClass As PeriodClass = PeriodClassDataUtils.findByKey(classId)
                Dim tval As Integer
                Dim maxGrade As Integer = periodClass.MaxGrade.GetValueOrDefault
                Dim errMsg As String = "Grade must be a numeric integer greater than 0 and less than or equal to " & maxGrade

                If Integer.TryParse(e.FormattedValue.ToString(), tval) = False Then
                    grdGrades.Rows(e.RowIndex).ErrorText = errMsg
                    e.Cancel = True

                Else
                    If tval <= 0 OrElse tval > maxGrade Then
                        grdGrades.Rows(e.RowIndex).ErrorText = errMsg
                        e.Cancel = True

                    End If
                End If
            End If

        End If

    End Sub

    Private Sub CellEndEdit(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

        If Me.grdGrades.DataLoading Then Exit Sub

        ' Clear the row error in case the user presses ESC.   
        grdGrades.Rows(e.RowIndex).ErrorText = String.Empty

    End Sub


    Private Sub SaveRowToStore(ByVal ThisDataRow As Object)
        If Me.grdGrades.DataLoading Then Exit Sub

        Dim c As ClassMember = ClassMemberDataUtils.loadFromDataRow(CType(ThisDataRow, DataRow))
        ClassMemberDataUtils.saveClassMember(c)

    End Sub


End Class